package com.minutes.in.seconds.ui.event;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.minutes.in.seconds.R;
import com.minutes.in.seconds.common.Constants;
import com.minutes.in.seconds.contacts.ContactsProvider;

public class AddContactEventHandler implements OnClickListener {

	private View root;

	private Bitmap contactsBitmap;

	public AddContactEventHandler(View root) {
		this.root = root;
	}

	@Override
	public void onClick(View v) {
		final Activity activity = ((Activity) v.getContext());

		switch (v.getId()) {
		case R.id.profile_photo:
			Intent intent = new Intent(Intent.ACTION_PICK);
			intent.setType("image/*");
			activity.startActivityForResult(intent, Constants.PICK_PHOTO_FLAG);
			break;

		case R.id.add_contact:
			final String name = ((EditText) root.findViewById(R.id.name)).getText().toString();
			final String lastName = ((EditText) root.findViewById(R.id.lastname)).getText().toString();
			final String phoneNumber = ((EditText) root.findViewById(R.id.mobile)).getText().toString();
			final String email = ((EditText) root.findViewById(R.id.email)).getText().toString();

			if (this.validateData(v.getContext(), name, lastName, phoneNumber, email)) {
				if (isContactExisted(activity, phoneNumber)) {
					new AlertDialog.Builder(activity).setTitle(activity.getString(R.string.warning))
							.setMessage(activity.getString(R.string.warning_contact_existed)).setIcon(android.R.drawable.ic_dialog_alert)
							.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog, int whichButton) {
									ContactsProvider.addPersonToContacts(activity, 0, name, lastName, phoneNumber, email, contactsBitmap);
									Toast.makeText(activity, "Contact added successfully!", Toast.LENGTH_SHORT).show();
								}

							}).setNegativeButton(android.R.string.no, null).show();
				}
			}
			break;
		}
	}

	public void setBitmap(Bitmap bitmap) {
		this.contactsBitmap = bitmap;
	}

	private boolean validateData(Context context, String name, String lastName, String mobilePhone, String email) {
		if (name.trim().length() == 0 || lastName.trim().length() == 0 || mobilePhone.trim().length() == 0 || email.trim().length() == 0) {
			Toast.makeText(context, "All fields are required.", Toast.LENGTH_SHORT).show();
			return false;
		} else {
			return true;
		}
	}

	public boolean isContactExisted(Activity _activity, String phoneNumber) {
		Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
		String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
		Cursor cur = _activity.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
		try {
			if (cur.moveToFirst()) {
				return true;
			}
		} finally {
			if (cur != null)
				cur.close();
		}
		return false;
	}
}
